package com.xuanyuanxinyu.core.base

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import java.util.*


/**
 * @author waynie
 * @date 2022/10/20
 * Describe : Activity管理
 */
/**
 * @FileName: ActivityController
 * @Description: Activity管理
 * @author ken.luo
 * @date 2023/4/4
 * @version V1.0
 * @email 424533553@qq.com
 */
object ActivityController {
    //    companion object {
    //        private var activityController: ActivityController? = null
    //
    //        @JvmStatic
    //        val instance: ActivityController
    //            get() {
    //                if (activityController == null) {
    //                    activityController = ActivityController()
    //                }
    //                return activityController!!
    //            }
    //    }
    //Stack(栈)，后进先出
    private val activityStack = Stack<Activity>()
    
    /**
     * 获得当前栈顶Activity
     */
    val currentActivity : Activity?
        get() {
            var activity : Activity? = null
            if (!activityStack.isEmpty()) activity = activityStack.peek()
            return activity
        }
    
    /**
     * 获得当前Activity名字,带包名
     */
    val currentActivityClassName : String
        get() {
            val activity = currentActivity
            var name = ""
            if (activity != null) {
                name = activity.componentName.className
            }
            return name
        }
    
    /**
     * 获得当前Activity名字，simple name
     */
    val currentActivityName : String
        get() {
            val activity = currentActivity
            var name = ""
            if (activity != null) {
                name = activity.componentName.shortClassName
            }
            return name.substring(name.lastIndexOf(".") + 1,name.length)
        }
    
    fun addActivity(activity : Activity) {
        activityStack.remove(activity)
        activityStack.push(activity)
    }
    
    /**
     * remove Activity
     */
    fun removeActivity(activity : Activity) {
        activityStack.remove(activity)
    }
    
    /**
     * 启动指定Activity 参数可选，intent.get***Extra 即可获取参数
     */
    fun start(clazz : Class<out Activity>,bundle : Bundle = Bundle()) {
        val intent = Intent(currentActivity,clazz)
        intent.putExtras(bundle)
        currentActivity?.startActivity(intent)
    }
    
    /**
     * 关闭指定Activity
     */
    fun finish(activity : Activity?) {
        if (activity != null) {
            activityStack.remove(activity)
            activity.finish()
        }
    }
    
    /**
     * 关闭所有Activity
     */
    fun closeAllActivity() {
        while (true) {
            val activity = currentActivity ?: break
            finish(activity)
        }
    }
    
    fun closeActivityByName(name : String?) {
        var index = activityStack.size - 1
        while (true) {
            val activity = activityStack[index] ?: break
            val activityName = activity.componentName.className
            if (!TextUtils.equals(name,activityName)) {
                index--
                if (index < 0) {
                    break
                }
                continue
            }
            finish(activity)
            break
        }
    }
    
    fun findActivityByName(name : String?) : Activity? {
        return activityStack.find { activity->
            val activityName = activity.componentName.className
            TextUtils.equals(name,activityName)
        }
    }
    
    fun getActivityStack() : Stack<Activity> {
        val stack = Stack<Activity>()
        stack.addAll(activityStack)
        return stack
    }
    
    /**
     * 根据时间休眠然后关闭当前页面
     * 比如：5秒自动返回
     * 或者只需要后台给一个结果而已
     */
    fun finishBySleep(millis : Long) {
        object : Thread() {
            override fun run() {
                try {
                    sleep(millis)
                    currentActivity?.finish()
                } catch (e : InterruptedException) {
                    e.printStackTrace()
                }
            }
        }.start()
    }
    
}